package com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.examples.pictureInPictureExample;

import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.utils.YouTubePlayerUtils;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.sampleapp.utils.VideoIdsProvider;
import com.pierfrancescosoffritti.aytplayersample.R;

public class PictureInPictureActivity extends AppCompatActivity {

  private YouTubePlayerView youTubePlayerView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picture_in_picture_example);

    initYouTubePlayerView();
  }

  private void initYouTubePlayerView() {
    youTubePlayerView = findViewById(R.id.youtube_player_view);

    getLifecycle().addObserver(youTubePlayerView);
    initPictureInPicture();

    youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
      @Override
      public void onReady(@NonNull YouTubePlayer youTubePlayer) {
        YouTubePlayerUtils.loadOrCueVideo(
                youTubePlayer, getLifecycle(),
                VideoIdsProvider.getNextVideoId(), 0f
        );
      }
    });
  }

  private void initPictureInPicture() {
    Button enterPipMode = findViewById(R.id.enter_pip);
    enterPipMode.setOnClickListener(view -> {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        boolean supportsPIP = getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
        if (supportsPIP)
          enterPictureInPictureMode();
      } else {
        new AlertDialog.Builder(this)
                .setTitle("Can't enter picture in picture mode")
                .setMessage("In order to enter picture in picture mode you need a SDK version >= N.")
                .show();
      }
    });
  }

  @Override
  public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
    super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);

    if (isInPictureInPictureMode) {
      youTubePlayerView.matchParent();
    } else {
      youTubePlayerView.wrapContent();
    }
  }
}
